// JavaScript Document
// tabledeleterow.js version 1.2 2006-02-21
// mredkj.com

// CONFIG notes. Below are some comments that point to where this script can be customized.
// Note: Make sure to include a <tbody></tbody> in your table's HTML

var INPUT_NAME_PREFIX = 'inputName'; // this is being set via script
var RADIO_NAME = 'totallyrad'; // this is being set via script
var TABLE_NAME = 'tblSample'; // this should be named in the HTML
var ROW_BASE = 1; // first number (for display)
var hasLoaded = false;
var curr_row = 0;
window.onload=fillInRows2;

function fillInRows(numrows)
{
	var i;
	hasLoaded = true;
	//alert ('Vao ham');
	if (document.getElementById && document.getElementsByTagName) {
		//alert ('Vao if');
		var tbl = document.getElementById(TABLE_NAME);
		var l = tbl.rows.length;
		//alert (l);
		if(l > 1){
			delallrow(tbl, l);
		}
		if(numrows){
			/*for(i=0;i<numrows;i++)
				addRowToTable(null);
			}*/
			//alert (numrows);
			i = 0;
			while(i<numrows){
				addRowToTable2(null,'','','','','','','','','','');
				//alert (i);
				i++;
				//alert (numrows);
			}
		}
	}
}
function fillInRows2(){
	hasLoaded = true;
}
// CONFIG:
// myRowObject is an object for storing information about the table rows
function myRowObject(one, two, three, four, five, six, seven, eight, nine,ten)
{
	this.one = one; // text object
	this.two = two; // input text object
	this.three = three; // input checkbox object
	this.four = four; // input radio object
	this.five = five;
	this.six = six;
	this.seven = seven;
	this.eight = eight;	
	this.nine = nine;	
	this.ten = ten;
}

/*
 * addRowToTable
 * Inserts at row 'num', or appends to the end if no arguments are passed in. Don't pass in empty strings.
 */

function addRowToTable2(num,VNDESC,ENDESC,ISDFT,LSCODE,LSVAL,LSDESC,CM2POS)
{	
	//alert(hasLoaded);
	if (hasLoaded && document.getElementById && document.getElementsByTagName) {

	 	//alert('ho tro dom');	
		var tbl = document.getElementById(TABLE_NAME);
	
		var nextRow = tbl.tBodies[0].rows.length;
		
		var iteration = nextRow + ROW_BASE;
		if (num == null) { 
			num = nextRow;
		} else {
			iteration = num + ROW_BASE;
		}
		// add the row
		
		var row = tbl.tBodies[0].insertRow(num);
		
		// CONFIG: requires classes named classy0 and classy1
		//row.className = 'classy' + (iteration % 2);
	
		// CONFIG: This whole section can be configured
		 //$VNDESC$ENDESC$ISDFT$LSCODE$LSVAL$LSDESC
		// cell 0 - text
		var cell0 = row.insertCell(0);
		cell0.setAttribute('align','center');
		cell0.style.padding = '.0em 0em';
		var txtNum = document.createElement('input');
		txtNum.setAttribute('type', 'text');
		txtNum.setAttribute('name', 'STT' + iteration);
		txtNum.setAttribute('id', 'STT' + iteration);
		txtNum.setAttribute('size', '4');
		txtNum.setAttribute('value', iteration); // iteration included for debug purposes				
		txtNum.setAttribute('readOnly', true);	
		txtNum.className = 'textcenter';
		cell0.appendChild(txtNum);
					
		var cell1 = row.insertCell(1);		
		cell1.style.padding = '.0em 0px';
		var txtInp1 = document.createElement('input');
		txtInp1.setAttribute('type', 'text');
		txtInp1.setAttribute('name', 'VNDESC' + iteration);
		txtInp1.setAttribute('id', 'VNDESC' + iteration);
		txtInp1.setAttribute('size', '25');
		txtInp1.className = 'textcenter';
		txtInp1.setAttribute('value', VNDESC); // iteration included for debug purposes		
		txtInp1.setAttribute('readOnly', true);
		cell1.appendChild(txtInp1);
				
		var cell2 = row.insertCell(2);
		cell2.style.padding = '.0em 0px';
		var txtInp2 = document.createElement('input');
		txtInp2.setAttribute('name', 'ENDESC' + iteration);
		txtInp2.setAttribute('id', 'ENDESC' + iteration);
		txtInp2.setAttribute('type', 'text');
		txtInp2.setAttribute('value', ENDESC);
		txtInp2.setAttribute('size', '25');
		txtInp2.setAttribute('readOnly', true);				
		cell2.appendChild(txtInp2);
		
		var cell3 = row.insertCell(3);		
		cell3.style.padding = '.0em 0px';
		var txtInp3 = document.createElement('input');
		txtInp3.setAttribute('type', 'text');
		txtInp3.setAttribute('name', 'ISDFT' + iteration);
		txtInp3.setAttribute('size', '18');		
		txtInp3.setAttribute('value', ISDFT); // iteration included for debug purposes
		txtInp3.setAttribute('readOnly', true);
		txtInp3.className = 'textcenter';
		cell3.appendChild(txtInp3);
		
		var cell4 = row.insertCell(4);
		//cell5.setAttribute('align','center');
		cell4.style.padding = '.0em 0px';
		var txtInp4 = document.createElement('input');
		txtInp4.setAttribute('type', 'text');
		txtInp4.setAttribute('name', 'LSCODE' + iteration);
		txtInp4.setAttribute('id', 'LSCODE' + iteration);
		txtInp4.setAttribute('size', '10');
		txtInp4.setAttribute('value', LSCODE); // iteration included for debug purposes		
		txtInp4.setAttribute('readOnly', true);
		cell4.appendChild(txtInp4);
		
		var cell5 = row.insertCell(5);
		//cell6.setAttribute('align','center');
		cell5.style.padding = '.0em 0px';
		var txtInp5 = document.createElement('input');
		txtInp5.setAttribute('type', 'text');
		txtInp5.setAttribute('id', 'LSVAL' + iteration);
		txtInp5.setAttribute('name', 'LSVAL' + iteration);
		txtInp5.setAttribute('size', '15');		
		txtInp5.setAttribute('value', LSVAL); // iteration included for debug purposes
		txtInp5.setAttribute('readOnly', true);
		cell5.appendChild(txtInp5);
		
		var cell6 = row.insertCell(6);
		//cell7.setAttribute('align','center');
		cell6.style.padding = '.0em 0px';
		var txtInp6 = document.createElement('input');
		txtInp6.setAttribute('type', 'text');
		txtInp6.setAttribute('name', 'LSDESC' + iteration);
		txtInp6.setAttribute('size', '25');		
		txtInp6.setAttribute('value', LSDESC); // iteration included for debug purposes
		txtInp6.setAttribute('readOnly', LSDESC);
		cell6.appendChild(txtInp6);
			
		var cell7 = row.insertCell(7);
		cell7.setAttribute('align','center');
		cell7.style.padding = '.0em 0px';
		var txtInp7 = document.createElement('input');
		txtInp7.setAttribute('type', 'text');
		txtInp7.setAttribute('name', 'CM2POS' + iteration);
		txtInp7.setAttribute('size', '25');		
		txtInp7.setAttribute('value', CM2POS); // iteration included for debug purposes
		txtInp7.setAttribute('readOnly', CM2POS);
		txtInp7.className = 'textcenter';
		cell7.appendChild(txtInp7);
		
		//var cell7 = row.insertCell(7);		
		//cell7.style.padding = '.0em 0px';
		//var txtInp7 = document.createElement('input');
		//txtInp7.setAttribute('type', 'text');
		//txtInp7.setAttribute('name', 'CM2POS' + iteration);
		//txtInp7.setAttribute('size', '25');		
		//txtInp7.setAttribute('value', CM2POS); // iteration included for debug purposes
		//txtInp7.setAttribute('readOnly', true);
		//txtInp7.className = 'textcenter';
		//cell7.appendChild(txtInp7);
			
			
		var cell8 = row.insertCell(8);
		//cell8.setAttribute('align','center');
		cell8.style.padding = '.0em 2px';
		var btnE2 = document.createElement('input');
		btnE2.setAttribute('type', 'button');
		btnE2.setAttribute('value', '     ');
		btnE2.setAttribute('id','replacement_edit');
		btnE2.onclick = function () {editCurrentRow(this)};
		cell8.appendChild(btnE2);
		
		var cell9 = row.insertCell(9);
		//cell9.setAttribute('align','center');
		cell9.style.padding = '.0em 2px';
		var btnE3 = document.createElement('input');
		btnE3.setAttribute('type', 'button');
		btnE3.setAttribute('value', '     ');
		btnE3.setAttribute('id','replacement_delete');
		btnE3.onclick = function () {deleteCurrentRow(this)};
		cell9.appendChild(btnE3);				
		// Pass in the elements you want to reference later
		// Store the myRow object in each row
		row.myRow = new myRowObject(txtNum, txtInp1, txtInp2, txtInp3, txtInp4, txtInp5, txtInp6,txtInp7,  btnE2, btnE3);
		var textbox = document.getElementById('TOTAL');
		textbox.value = tbl.tBodies[0].rows.length;
	}
}

// CONFIG: this entire function is affected by myRowObject settings

// If there isn't a checkbox in your row, then this function can't be used.
function deleteCurrentRow(obj)
{
//	alert('vao ham');
	if (hasLoaded) {
		var delRow = obj.parentNode.parentNode;
		var tbl = delRow.parentNode.parentNode;
		var rIndex = delRow.sectionRowIndex;
		var rowArray = new Array(delRow);
		deleteRows(rowArray);
		reorderRows(tbl, rIndex);
	}

}
function editCurrentRow(obj)
{
	//alert('vao ham edit');
	var capnhat = document.getElementById('capnhathrcmlists');
	var themmoi = document.getElementById('themmoihrcmlists');
	if(capnhat && themmoi){
		capnhat.disabled = false;
		themmoi.disabled = true;
		var thisRow = obj.parentNode.parentNode;
		var tbl = thisRow.parentNode.parentNode;
		var rIndex = thisRow.sectionRowIndex;
		curr_row = rIndex;
		//$STYPE|$GRPSEQ|$GRPDESC|$SEQNO|$FLDNAME|$FORMSTR|$REMARK
		//var RPTID =  document.getElementById("RPTID");
		var VNDESC =  document.getElementById("VNDESC");
		var ENDESC =  document.getElementById("ENDESC");
		var ISDFT =  document.getElementById("ISDFT");
		var LSCODE =  document.getElementById("LSCODE");
		var LSVAL =  document.getElementById("LSVAL");
		var LSDESC =  document.getElementById("LSDESC");
		var CM2POS =  document.getElementById("CM2POS");
							
		if(LSCODE){
			//RPTID.value = tbl.tBodies[0].rows[rIndex].myRow.two.value;
			VNDESC.value = tbl.tBodies[0].rows[rIndex].myRow.two.value;						
			ENDESC.value = tbl.tBodies[0].rows[rIndex].myRow.three.value;			
			ISDFT.value = tbl.tBodies[0].rows[rIndex].myRow.four.value;
			LSCODE.value = tbl.tBodies[0].rows[rIndex].myRow.five.value;
			LSVAL.value = tbl.tBodies[0].rows[rIndex].myRow.six.value;
			LSDESC.value = tbl.tBodies[0].rows[rIndex].myRow.seven.value;	
			CM2POS.value = tbl.tBodies[0].rows[rIndex].myRow.eight.value;
		}
	}
	
}
/*
function orderRow(tbl,startingIndex){
	if (hasLoaded) {
		if (tbl.tBodies[0].rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
					
				tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}
}
*/
function reorderRows(tbl, startingIndex)
{
	if (hasLoaded) {
		//document.maphat.TERMNO.value =  tbl.tBodies[0].rows.length;
		/*var textbox = document.maphat.TERMNO;
		if(textbox){
			alert('Co');	
		}else{
			alert('Khong');
		}*/
						
		var textbox = document.getElementById('TOTAL');
		textbox.value = tbl.tBodies[0].rows.length;
		//alert(tbl.tBodies[0].rows.length);
		if (tbl.rows[startingIndex]) {
			var count = startingIndex + ROW_BASE;
			for (var i=startingIndex; i<tbl.tBodies[0].rows.length; i++) {
			
				// CONFIG: next line is affected by myRowObject settings
				tbl.tBodies[0].rows[i].myRow.one.data = count; // text
				//$GRPSEQ|$GRPDESC|$SEQNO|$FLDNAME|$FORMSTR|$REMARK
				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.two.name = 'RPTID' + count; // input text
				tbl.tBodies[0].rows[i].myRow.two.name = 'VNDESC' + count; // input text
				tbl.tBodies[0].rows[i].myRow.three.name = 'ENDESC' + count; // input text
				tbl.tBodies[0].rows[i].myRow.four.name = 'ISDFT' + count; // input text
				tbl.tBodies[0].rows[i].myRow.five.name = 'LSCODE' + count; // input text
				tbl.tBodies[0].rows[i].myRow.six.name = 'LSVAL' + count; // input text
				tbl.tBodies[0].rows[i].myRow.seven.name = 'LSDESC' + count; // input text
				tbl.tBodies[0].rows[i].myRow.eight.name = 'CM2POS' + count; // input text
				//tbl.tBodies[0].rows[i].myRow.eight.name = 'SQLSTR' + count; // input text
				//tbl.tBodies[0].rows[i].myRow.two.value = 'TERM' + count; // input text
				// CONFIG: next line is affected by myRowObject settings
				//var tempVal = tbl.tBodies[0].rows[i].myRow.two.value.split(' '); // for debug purposes
				//tbl.tBodies[0].rows[i].myRow.two.value = count + ' was' + tempVal[0]; // for debug purposes
				
				// CONFIG: next line is affected by myRowObject settings
				//tbl.tBodies[0].rows[i].myRow.four.value = count; // input radio
				
				// CONFIG: requires class named classy0 and classy1
				//tbl.tBodies[0].rows[i].className = 'classy' + (count % 2);
				
				count++;
			}
		}
	}

}

function deleteRows(rowObjArray)
{
	if (hasLoaded) {
		for (var i=0; i<rowObjArray.length; i++) {
			var rIndex = rowObjArray[i].sectionRowIndex;
			rowObjArray[i].parentNode.deleteRow(rIndex);
		}
	}
	
}

function openInNewWindow(frm)
{
	// open a blank window
	var aWindow = window.open('', 'TableAddRow2NewWindow',
	'scrollbars=yes,menubar=yes,resizable=yes,toolbar=no,width=400,height=400');
	
	// set the target to the blank window
	frm.target = 'TableAddRow2NewWindow';
	
	// submit
	frm.submit();
}

function delallrow(moduleTable,l) {
len = l;
	for (i = len -1; i != 0; i--) {
	
		moduleTable.deleteRow(i);
	
	}
}
/*
function openwindow(form, index){
	mywindow = window.open('microbank_id.php?name=ACCTNO' + index+ '-CURCODE' + index, 'mywindow','menubar=0,resizable=yes,width=800,height=300, scrollbars=yes');
	mywindow.moveTo((screen.width-800)/2,screen.height/2);
	
	//window.open('microbank_id.php?name=ACCTNO' + index + '-CURCODE' + index);
}*/
//$GRPSEQ|$GRPDESC|$SEQNO|$FLDNAME|$FORMSTR|$REMARK
function addRow(){	

	//var RPTID =  document.getElementById("RPTID");
	var VNDESC =  document.getElementById("VNDESC");
	var ENDESC =  document.getElementById("ENDESC");
	var ISDFT =  document.getElementById("ISDFT");
	var LSCODE =  document.getElementById("LSCODE");
	var LSVAL =  document.getElementById("LSVAL");
	var LSDESC =  document.getElementById("LSDESC");
	var CM2POS = document.getElementById("CM2POS");
	//var SQLSTR =  document.getElementById("SQLSTR");
	//alert(CM2POS.checked);
		
	if(LSCODE){
		//alert('asd1');
		//var RPTIDtmp = RPTID.value;
		var VNDESCtmp = VNDESC.value;
		var ENDESCtmp = ENDESC.value;
		var ISDFTtmp = ISDFT.value;
		var LSCODEtmp = LSCODE.value;
		var LSVALtmp = LSVAL.value;
		var LSDESCtmp = LSDESC.value;
		var tmp = CM2POS.value;
		var CM2POStmp = tmp.substring(0,1);
		//if(CM2POS.checked){
			//CM2POStmp = 'Y';
		//}else{
			//CM2POStmp = 'N';
		//}
		
		//var SQLSTRtmp = SQLSTR.value;
		
		if(LSCODEtmp){
			//alert('asd2');
			addRowToTable2(null,VNDESCtmp,ENDESCtmp,ISDFTtmp,LSCODEtmp,LSVALtmp,LSDESCtmp,CM2POStmp);
			//RPTID.value = "";
			//VNDESC.value = "";
			//ENDESC.value="";
			ISDFT.value="";
			LSCODE.value="";
			LSVAL.value="0.0000";
			LSDESC.value="";			
		}else{
			alert('Vui lòng nhập mã số!');		
		}
	}
}

//$GRPSEQ|$GRPDESC|$SEQNO|$FLDNAME|$FORMSTR|$REMARK
function updateRow(){
	var capnhat = document.getElementById("capnhathrcmlists");
	var themmoi = document.getElementById("themmoihrcmlists");
	
	if(capnhat && themmoi){
		capnhat.disabled = true;
		themmoi.disabled = false;
		
		var tbl = document.getElementById(TABLE_NAME);
		rIndex = curr_row;
		if(rIndex>=0){
			
			//var RPTID =  document.getElementById("RPTID");
			var VNDESC =  document.getElementById("VNDESC");
			var ENDESC =  document.getElementById("ENDESC");
			var ISDFT =  document.getElementById("ISDFT");
			var LSCODE =  document.getElementById("LSCODE");
			var LSVAL =  document.getElementById("LSVAL");
			var LSDESC =  document.getElementById("LSDESC");
			var CM2POS = document.getElementById("CM2POS");
						
			if(LSCODE){				
				var VNDESCtmp = VNDESC.value;
				var ENDESCtmp = ENDESC.value;
				var ISDFTtmp = ISDFT.value;
				var LSCODEtmp = LSCODE.value;
				var LSVALtmp = LSVAL.value;
				var LSDESCtmp = LSDESC.value;
				var tmp = CM2POS.value;
				var CM2POStmp = tmp.substring(0,1);
				//if(CM2POS.checked){
					//CM2POStmp = 'Y';
				//}else{
				//	CM2POStmp = 'N';
				//}
				if(LSCODEtmp){
			
					//tbl.tBodies[0].rows[rIndex].myRow.two.value = RPTID.value;
					tbl.tBodies[0].rows[rIndex].myRow.two.value = VNDESCtmp;				
					tbl.tBodies[0].rows[rIndex].myRow.three.value =  ENDESCtmp ;
					tbl.tBodies[0].rows[rIndex].myRow.four.value = ISDFTtmp;
					tbl.tBodies[0].rows[rIndex].myRow.five.value = LSCODEtmp;
					tbl.tBodies[0].rows[rIndex].myRow.six.value = LSVALtmp;
					tbl.tBodies[0].rows[rIndex].myRow.seven.value = LSDESCtmp;
					tbl.tBodies[0].rows[rIndex].myRow.eight.value = CM2POStmp;	
					//RPTID.value = "";
					//VNDESC.value = "";
					//ENDESC.value="";
					ISDFT.value="";
					LSCODE.value="";
					LSVAL.value="";
					LSDESC.value="";				
				}else{
					alert('Vui lòng nhập mã số!');	
					capnhat.disabled = false;
					themmoi.disabled = true;
				}			
			}
		}
	}
}